package com.shuorikeji.huohuo.base;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.shuorikeji.huohuo.R;
import com.shuorikeji.huohuo.app.AppManager;
import com.shuorikeji.huohuo.utils.LoadingDialogManager;
import com.shuorikeji.huohuo.utils.ToastShowUtil;
import com.gyf.barlibrary.ImmersionBar;
import com.gyf.barlibrary.OnKeyboardListener;

import androidx.fragment.app.FragmentActivity;
import butterknife.ButterKnife;

/**
 * Created by wxl
 * 将Presenter封装进baseActivity
 */

public abstract class BasePresenterActivity<T extends BasePresenter> extends FragmentActivity implements BaseView {

    public Context mContext = null;


    public T mPresenter;

    private ImmersionBar mImmersionBar;
    public LoadingDialogManager loading;
    private KeyboardListener keyboardListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 添加Activity到堆栈
        AppManager.getAppManager().addActivity(this);
        mContext = this;
        setContentView();
        mPresenter = GenericUtil.getObj(this, 0);
        if (mPresenter != null) {
            mPresenter.attachView(this);
        }
        ButterKnife.bind(this);
        mImmersionBar = ImmersionBar.with(this).statusBarColor(R.color.white).keyboardEnable(true).setOnKeyboardListener(onKeyboardListener).
                statusBarDarkFont(true, 0.2f).fitsSystemWindows(true);
        mImmersionBar.init();
        ToastShowUtil.init();
        loading = LoadingDialogManager.create(this);
        initData();
        loadData();

    }

    protected abstract void setContentView();

    protected abstract void initData();

    protected void loadData() {

    }

    protected void setKeyboardListener(KeyboardListener listener) {
        keyboardListener = listener;
    }

    OnKeyboardListener onKeyboardListener = new OnKeyboardListener() {
        @Override
        public void onKeyboardChange(boolean isPopup, int keyboardHeight) {
            if (keyboardListener != null) {
                if (isPopup) {
                    keyboardListener.show();
                } else {
                    keyboardListener.hide();
                }
            }
        }
    };

    public interface KeyboardListener {
        void show();

        void hide();
    }

    public void showProgress() {
        if (loading != null) {
            loading.show();
        }
    }

    public void hideProgress() {
        if (loading != null) {
            loading.dismiss();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 结束Activity&从堆栈中移除
        if (mPresenter != null) {
            mPresenter.detachView();//取消订阅
        }
        if (mImmersionBar != null) {
            mImmersionBar.destroy();  //必须调用该方法，防止内存泄漏，不调用该方法，如果界面bar发生改变，在不关闭app的情况下，退出此界面再进入将记忆最后一次bar改变的状态
        }
        if (loading != null) {
            loading.destroy();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    /**
     * 实现字体不随系统调节改变
     *
     * @param newConfig
     */
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        if (newConfig.fontScale != 1)//非默认值
            getResources();
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        if (res.getConfiguration().fontScale != 1) {//非默认值
            Configuration newConfig = new Configuration();
            newConfig.setToDefaults();//设置默认
            res.updateConfiguration(newConfig, res.getDisplayMetrics());
        }
        return res;
    }

    public void hideSoftInput(Context context, View view) {
        InputMethodManager imm =
                (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm == null) return;
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }


    public ImmersionBar getmImmersionBar() {
        return mImmersionBar;
    }

}
